package com.xjw.controller;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjw.entity.UserInfo;

/**      
 * 项目名称：spring-project  
 * 实现功能： controller独立定义启动端口号 
 * 类名称：TestController   
 * 版权所有：
 * 创建人：徐纪伟 
 * E-mail: 289045706@qq.com
 * 创建时间：2016年12月12日 上午10:28:42   	
 * 修改人：   
 * 修改时间：   
 * 修改备注：   
 * @version    
 */
@EnableAutoConfiguration
@EnableScheduling
@RestController
@RequestMapping("/proxyApi")
public class TestController implements EmbeddedServletContainerCustomizer{

    
    public static void main(String[] args) {
    	SpringApplication.run(TestController.class);
    }

	@Override
	public void customize(ConfigurableEmbeddedServletContainer arg0) {
		arg0.setPort(8081);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping("/getUser")
    public UserInfo getUser() throws Exception{
		URL url = new URL("http://localhost:8080/user/getUser4.json");
		URLConnection connection = url.openConnection();
		InputStream inputStream = connection.getInputStream();
		byte[] result = new byte[inputStream.available()];
		inputStream.read(result);
		String jsonStr = new String(result);
		ObjectMapper mapper = new ObjectMapper();
		UserInfo userInfo = mapper.readValue(result, UserInfo.class);
    	return userInfo;
    }
	
	
}

